<?php

namespace Trawind\Basics;

use League\Fractal\TransformerAbstract;
use Trawind\Basics\Traits\HasSingleton;
use Trawind\Basics\Transformers\RemoteFieldTransform;

abstract class TrawindTransformer extends TransformerAbstract
{

    use HasSingleton;

    protected $businessTypeId;

    protected static $fillMode = RemoteFieldTransform::class;

    public function __construct(int $businessTypeId)
    {
        $this->businessTypeId = $businessTypeId;
    }


    /**
     * @note    指定树形顶节点字段，除去 id,parent_id字段的
     * @return array
     * @example ['name'=>'母公司','nember'=>'code1']
     */
    public function treeTopNode(): array
    {
        return [];
    }


    /**
     * @note 指定关联转换的字段与接口类映射关系
     * @param  array  $data
     * @return array
     */
    public static function transformMap(): array
    {
        return [];
    }


    /**
     * @note 处理个性化组装数据，此处是即将呈现给前端的最后一步，所以不会出现N+1问题，任意组装
     * @param  array  $data
     * @return array
     */
    public function diyFillTransformData(array $data)
    {
        return $data;
    }


    /**
     * @note 填充字段远程关联结构
     * @param  array  $data
     * @return array
     */
    public function fillTransformData(array $data)
    {
        $isMultiple = is_array(current($data['data']));

        $_data = $isMultiple ? $data['data'] : [$data['data']];
        $data = $this->diyFillTransformData(app(static::$fillMode)($_data, $this));
        return $isMultiple ? compact('data') : ['data' => current($data)];
    }

}